<?php

class Evenement{
  var $id = 0;
  var $evenement_info = null;
  function __construct($id){
      $this->id = $id;
      if ($id != "new")
        $this->_getEvenement();
    
  }

  function setEvenementInfo($titre,$debut,$fin,$debut_inscription,$fin_inscription,$nom_referent, $archive){
    if ($this->id != "new"){
      $this->modifierEvenement($titre,$debut,$fin,$debut_inscription,$fin_inscription,$nom_referent,$archive);
      $this->_getEvenement();
    }
    else{
      $this->id = $this->ajouterEvenement($titre,$debut,$fin,$debut_inscription,$fin_inscription,$nom_referent,$archive);
      $this->_getEvenement();
    }
  }
  
  function getEvenementInfo(){
    return $this->evenement_info;
  }
  
  private function ajouterEvenement($titre,$debut,$fin,$debut_inscription,$fin_inscription,$nom_referent,$archive){
  
    mysql_query("INSERT INTO ".BD_PREFIX."evenement (  titre_evenement,
                                debut_evenement, fin_evenement,
                                debut_inscription_evenement, fin_inscription_evenement,
                                crea_evenement,
                                nom_referent_evenement,
                                archive_evenement) 
                  VALUES(".clean_for_db($titre).",
                          ".clean_for_db($debut).",
                          ".clean_for_db($fin).",
                          ".clean_for_db($debut_inscription).",
                          ".clean_for_db($fin_inscription).",
                          ".clean_for_db(time()).",
                          ".clean_for_db($nom_referent).",
                          ".clean_for_db($archive).")");
    if (! $error = mysql_error())
      return mysql_insert_id();
    else{
      echo $error;
      return false;
    }
  }
  
  private function modifierEvenement($titre,$debut,$fin,$debut_inscription,$fin_inscription,$nom_referent,$archive){

    mysql_query("UPDATE ".BD_PREFIX."evenement
            SET titre_evenement = ".clean_for_db($titre).", 
              debut_evenement = $debut,
              fin_evenement = $fin,
              debut_inscription_evenement = $debut_inscription,
              fin_inscription_evenement = $fin_inscription,
              nom_referent_evenement = ".clean_for_db($nom_referent).",
              archive_evenement = $archive
            WHERE id_evenement = $this->id");
    if (! $error = mysql_error())
      return $this->id;
    else{
      echo $error;
      return false;
    }
  }
  
  private function _getEvenement(){
    $result = mysql_query("SELECT id_evenement AS id,
                    titre_evenement AS titre,
                    debut_evenement AS debut, 
                    fin_evenement AS fin,
                    debut_inscription_evenement AS debut_inscription, 
                    fin_inscription_evenement AS fin_inscription,
                    nom_referent_evenement AS nom_referent,
                    archive_evenement AS archive
                 FROM ".BD_PREFIX."evenement 
                 WHERE id_evenement = $this->id");
     $this->evenement_info = mysql_fetch_array($result); 
  }
  

  static function getListeEvenement($archive = false){
    $archive_sql = ($archive?"1":"0");
    $output = array();
    $result = mysql_query("SELECT id_evenement AS id,
                    titre_evenement AS titre, 
                    debut_evenement AS debut, 
                    fin_evenement AS fin,
                    debut_inscription_evenement AS debut_inscription, 
                    fin_inscription_evenement AS fin_inscription,  
                    nom_referent_evenement AS nom_referent,
                    archive_evenement AS archive  
                  FROM ".BD_PREFIX."evenement 
                  WHERE archive_evenement = $archive_sql
                  ORDER BY titre_evenement");
    echo mysql_error();
    while($data = mysql_fetch_array($result)){
      $output[] = $data; 
    }
    return $output; 
  }
  
  function getListeGestionnaires(){
    $output = array();
    if($this->id != "new"){  
      $result = mysql_query("SELECT id_membre FROM ".BD_PREFIX."evenement_gestion 
                             WHERE id_evenement = $this->id");
       echo mysql_error();                                               
      while($data = mysql_fetch_array($result)){
        $output[] = $data[0]; 
      }
    }
    return $output;
  }
  
  function ajouterGestionnaire($membre_id){
    mysql_query("INSERT INTO ".BD_PREFIX."evenement_gestion(id_membre,id_evenement) 
                  VALUES ($membre_id,$this->id)");
  }
  
  function supprimerGestionnaire($membre_id){
    mysql_query("DELETE FROM ".BD_PREFIX."evenement_gestion 
                  WHERE id_membre=$membre_id AND id_evenement=$this->id");
  }

}
?>